package com.jotta.client;

import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;

import com.jotta.client.communicate.Configuration;
import com.jotta.client.utility.Utility;

public class MusicPlayer extends Activity {
	private String fileName;
	private String filePath;
	private AudioView musicPlayer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_view);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			fileName = extras.getString(Configuration.FILE_NAME);
			filePath = extras.getString(Configuration.FILE_PATH);
		}

		updateTitle(fileName);
		setResourceForMediaPlayer(filePath);
	}

	private void updateTitle(String fileName) {
		String app_name = Configuration.getRes().getString(R.string.app_name);
		setTitle(app_name + " - " + fileName);
	}

	private void setResourceForMediaPlayer(String filePath) {
		try {
			musicPlayer = new AudioView(MusicPlayer.this);
			musicPlayer
					.setMediaController(new MediaController(MusicPlayer.this));
			musicPlayer.setVideoPath(filePath);
			musicPlayer.setImageResource(R.drawable.slapscreen);
			musicPlayer.start();
			setContentView(musicPlayer);
		} catch (Exception e) {
			Utility.getAlertDialog(MusicPlayer.this, Configuration.getRes(),
                    e.getMessage()).show();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (musicPlayer != null) {
			musicPlayer.stopPlayback();
			musicPlayer = null;
		}
	}

}
